package com.h2View.utils;

import com.h2View.db.dao.SqlMapper;
import com.h2View.exception.ServiceException;
import com.xjw.utils.LogUtil;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author xwx1052336
 * @since iFad Platform V100R001,2021/8/24
 */
public class ToolUtil {

    private static Log log = LogFactory.get();

    public static Boolean copyFlag = false;

    public static void main(String[] args) throws Exception {

    }

    public static boolean isEmpty(Collection<?> src){
        return src == null || src.size() == 0;
    }

    public static boolean isEmpty(Map src){
        return src == null || src.size() == 0;
    }

    public static void copyValue(ListView<?> dataEle){
        if(!copyFlag){
            return;
        }
        String value = dataEle.getSelectionModel().getSelectedItem().toString();
        ClipboardContent content = new ClipboardContent();
        content.putString(value);
        Clipboard clipboard = Clipboard.getSystemClipboard();
        clipboard.setContent(content);
    }



    public static void copyValue(TableView<?> dataEle){
        if(!copyFlag){
            return;
        }
        ObservableList<TablePosition> tablePositions = dataEle.getSelectionModel().getSelectedCells();
        if(isEmpty(tablePositions)){
            return;
        }
        TablePosition pos = tablePositions.get(0);
        int row = pos.getRow();
        TableColumn col = pos.getTableColumn();
        Object value = col.getCellObservableValue(dataEle.getItems().get(row)).getValue();
        if(StringUtil.isEmpty(value)){
            return;
        }
        ClipboardContent content = new ClipboardContent();
        content.putString(value.toString());
        Clipboard clipboard = Clipboard.getSystemClipboard();
        clipboard.setContent(content);
    }

    public static void execAsync(Runnable r1, Runnable r2){
        ThreadUtil.execAsync(()->{
            r1.run();
            Platform.runLater(r2);
        });
    }


    public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
        T t = null;
        try {
            t = beanClass.newInstance();
        } catch (Exception e) {
            log.error(e);
            throw new ServiceException("创建对象失败");
        }
        switch (SqlUtil.getDataType().getDataType()){
            case 1:
                for(String key:map.keySet()) {
                    if(ReflectUtil.getField(beanClass,key.toLowerCase())!=null){
                        ReflectUtil.setFieldValue(t,key.toLowerCase(),map.get(key));
                    }
                }
                break;
            case 2:
                for(String key:map.keySet()) {
                    ReflectUtil.setFieldValue(t,key,map.get(key));
                }
                break;
            default:break;
        }
        return t;
    }

    public static void execAsync(Runnable runnable){
        ThreadUtil.execAsync(()->{
            Platform.runLater(runnable);
        });
    }

    public static void execAsync(Runnable runnable,long time){
        ThreadUtil.execAsync(()->{
            ThreadUtil.sleep(time);
            Platform.runLater(runnable);
        });
    }

    public static void addTableColumns(String key,TableView<LinkedHashMap<String,Object>> tableEle){
        TableColumn<LinkedHashMap<String,Object>,String> tablesColumn = new TableColumn<>(key);
        tablesColumn.setCellValueFactory(p -> {
            Object value = p.getValue().get(key);
            if(!StringUtil.isEmpty(value)){
                return new SimpleStringProperty(value.toString());
            }else{
                return new SimpleStringProperty(null);
            }
        });
        tableEle.getColumns().add(tablesColumn);
    }

    @SuppressWarnings("unchecked")
    public static <T> T cast(Object value){
        try{
            return (T)value;
        }catch (Exception e){
            LogUtil.error(e);
            return null;
        }
    }

    public static boolean excute(String sql){
        try{
            SqlMapper sqlMapper = MybatisUtils.getMapper(SqlMapper.class);
            sqlMapper.customQueryItem(sql);
            return true;
        }catch (Exception e){
            log.error(e);
            return false;
        }finally {
            MybatisUtils.reLoadSession();
        }
    }

    public static StringBuilder deleteLast(StringBuilder stb){
        if(stb.length() == 0){
            return stb;
        }
        stb.delete(stb.length()-1,stb.length());
        return stb;
    }

    public static StringBuilder deleteLast(StringBuilder stb,int size){
        if(stb.length() == 0){
            return stb;
        }
        stb.delete(stb.length()-size,stb.length());
        return stb;
    }

}
